package ynu.edu.api.client.fallback;

import io.github.resilience4j.circuitbreaker.CallNotPermittedException;
import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import ynu.edu.api.client.PayClient;
import ynu.edu.api.dto.PayOrderDTO;

@Slf4j
@Component
public class PayClientFallback implements FallbackFactory<PayClient> {
    @Override
    public PayClient create(Throwable cause) {
        return new PayClient() {
            @Override
            public PayOrderDTO queryPayOrderByBizOrderNo(Long id) {
                if (cause instanceof CallNotPermittedException) {
                    log.error("熔断器触发，拒绝请求，订单ID：{}", id);
                } else if (cause instanceof RequestNotPermitted) {
                    log.error("限流器触发，拒绝请求，订单ID：{}", id);
                } else {
                    log.error("查询支付单失败，订单ID：{}，异常：{}", id, cause.getMessage());
                }
                return null;
            }
        };
    }
}